package com.gzc.pm.logic
{
	import cn.skyclass.business.SessionManager;
	import cn.skyclass.net.BaseLoader;
	
	import com.gzc.pm.bean.DetsetbBean;
	import com.gzc.pm.bean.ProcessRouteBean;
	import com.gzc.pm.dao.DetsetbDao;
	import com.gzc.pm.dao.ProcessRouteDao;
	import com.gzc.pm.dao.ToleranceDao;
	
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.ResultEvent;
	
	public class ProcEquLoader extends BaseLoader
	{
		public function ProcEquLoader(plId:int = 0)
		{
			super();
			_plId = plId;
		}
		
		private var _plId:int;
		
		[Bindable] public var procList:ArrayCollection;//工序列表
		private var _procIdsStr:String;
		
//		public var equList:ArrayCollection;//工序下的设备列表
//		public var procEquDict:Dictionary;//工序设备字典，procCode->equList
//		private var _equIdsStr:String;
		
//		public var equconList:ArrayCollection;//设备检测项列表
//		public var equconDict:Dictionary;//设备检测项字典，equipmentId->equconList
		public var detsetbList:ArrayCollection;//检测项列表
		public var detsetbDict:Dictionary;//检测项字典，procId->detsetbList
		
		public function get procIds():String{
			return _procIdsStr;
		}
		
		override protected function doLoad():void{
			var procDao:ProcessRouteDao = new ProcessRouteDao();
			procDao.getInfoByPlId(_onProcLoaded, _plId);
		}
		private function _onProcLoaded(e:ResultEvent):void{
			procList = e.result as ArrayCollection;
			if(_plId == ProcessRouteBean.LZ_LINE_ID){//轮轴生产线，添加两个工序
				procList.addAll(ProcessRouteBean.lunzhouRoutes);
			}
			
//			procEquDict = new Dictionary();
			detsetbDict = new Dictionary();
			_procIdsStr = "";
			for each(var proc:ProcessRouteBean in procList){
				_procIdsStr += proc.prId + ",";
//				procEquDict[proc.prId] = new ArrayCollection();
				detsetbDict[proc.prId] = new ArrayCollection();
			}
			_procIdsStr += "0";
			
//			_loadEqu();
			_loadDetsetb();
		}
		
		//加载检测项配置
		private function _loadDetsetb():void{
			var detsetbDao:DetsetbDao = new DetsetbDao();
			detsetbDao.getInPrIds(_procIdsStr, _onDetsetbLoaded);
		}
		private function _onDetsetbLoaded(e:ResultEvent):void{
			detsetbList = e.result as ArrayCollection;
			if(_plId == ProcessRouteBean.LZ_LINE_ID){//轮轴生产线，添加轮轴工序下的检测项
				detsetbList.addAll(DetsetbBean.ldyzDetsetbs);
				detsetbList.addAll(DetsetbBean.zjDetsetbs);
			}
			
			for each(var detsetb:DetsetbBean in detsetbList){
				var detsetbs:ArrayCollection = detsetbDict[detsetb.prId];
				if(detsetbs){
					detsetbs.addItem(detsetb);
				}
			}
			
			SessionManager.current.setAttribute('procEquData', this);
			dispatchLoadedEvent();
		}
		
		//加载设备信息
//		private function _loadEqu():void{
//			var equDao:EquipmentDao = new EquipmentDao();
//			equDao.getInProcIds(_procIdsStr, _onEquLoaded);
//		}
//		private function _onEquLoaded(e:ResultEvent):void{
//			equList = e.result as ArrayCollection;
//			
//			equconDict = new Dictionary();
//			_equIdsStr = "";
//			for each(var equ:EquipmentBean in equList){
//				_equIdsStr += equ.equId+",";
//				equconDict[equ.equId] = new ArrayCollection();
//				
//				var equList:ArrayCollection = procEquDict[equ.prId];
//				if(equList){
//					equList.addItem(equ);
//				}
//			}
//			_equIdsStr += "-1";
//			
//			_loadEqucon();
//		}
		
		//加载设备检测项配置
//		private function _loadEqucon():void{
//			var equconDao:EquconDao = new EquconDao();
//			equconDao.getInEquIds(_equIdsStr, _onEquconLoaded);
//		}
//		private function _onEquconLoaded(e:ResultEvent):void{
//			equconList = e.result as ArrayCollection;
//			
//			for each(var equcon:EquconBean in equconList){
//				var equcons:ArrayCollection = equconDict[equcon.equipmentId];
//				if(equcons){
//					equcons.addItem(equcon);
//				}
//			}
//			
//			SessionManager.current.setAttribute('procEquData', this);
//			dispatchLoadedEvent();
//		}
		
	}
}